<?php

namespace App\Models;

use App\Models\Member\Member;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\DefaultDatetimeFormat;

class Ass extends Model
{
    use DefaultDatetimeFormat, ModelTree;

    protected $table = 'ass';

    public $sortable = [
        'order_column_name' => 'order',
        'sort_when_creating' => true,
    ];

    protected $fillable = ['no', 'content', 'member_id', 'member_names',  'obj_id',  'lat',  'lng',  'imgs',  'address',  'type',   'org_id',    'work_org_id', ];


    protected function getImgsAttribute($value)
    {
        return json_decode($value, true);
    }

    public static function grid($callback)
    {
        return new Grid(new static, $callback);
    }

    public static function form($callback)
    {
        return new Form(new static, $callback);
    }

    public function obj()
    {
        return $this->belongsTo(Obj::class, 'obj_id', 'id');
    }

    public function member()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

    public function assMembers()
    {
        return $this->hasMany(AssMember::class, 'ass_id', 'id');
    }

    public function assMember()
    {
        return $this->hasOne(AssMember::class, 'ass_id', 'id');
    }

    public function org()
    {
        return $this->belongsTo(Org::class, 'org_id', 'id');
    }
}
